//
//  SignUpViewController.swift
//  RXmvvmDemo
//
//  Created by jemi on 2020/1/3.
//  Copyright © 2020 jemi. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit

class SignUpViewController: UIViewController {
    
    let usernameTF = UITextField()
    let usernameLabel = UILabel()
    let passwordTF = UITextField()
    let passwordLabel = UILabel()
    let passwordReapedTF = UITextField()
    let passwordReapedLabel = UILabel()
    let loginBtn = UIButton()
    
    let disposBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadUI()
    }
    
    func loadUI() {
        self.view.addSubview(usernameTF)
        usernameTF.placeholder = "请输入用户名"
        usernameTF.layer.borderWidth = 1
        usernameTF.layer.borderColor = UIColor.gray.cgColor
        usernameTF.layer.cornerRadius = 5
        usernameTF.clipsToBounds = true
        usernameTF.snp.makeConstraints { (m) in
            m.leading.equalTo(30)
            m.top.equalTo(100)
            m.trailing.equalTo(-30)
            m.height.equalTo(40)
        }
        
        self.view.addSubview(usernameLabel)
        usernameLabel.snp.makeConstraints { (m) in
            m.leading.equalTo(60)
            m.top.equalTo(usernameTF.snp_bottom).offset(10)
            m.trailing.equalTo(-60)
            m.height.equalTo(20)
        }
    
        
        self.view.addSubview(passwordTF)
        passwordTF.layer.borderWidth = 1
        passwordTF.layer.borderColor = UIColor.gray.cgColor
        passwordTF.layer.cornerRadius = 5
        passwordTF.clipsToBounds = true
        passwordTF.placeholder = "请输入密码"
        passwordTF.isSecureTextEntry = true
        passwordTF.snp.makeConstraints { (m) in
            m.leading.trailing.height.equalTo(usernameTF)
            m.top.equalTo(usernameLabel.snp_bottom).offset(10)
        }
        
        self.view.addSubview(passwordLabel)
        passwordLabel.snp.makeConstraints { (m) in
            m.leading.trailing.height.equalTo(usernameLabel)
            m.top.equalTo(passwordTF.snp_bottom).offset(10)
        }
        
        self.view.addSubview(passwordReapedTF)
        passwordReapedTF.layer.borderWidth = 1
        passwordReapedTF.layer.borderColor = UIColor.gray.cgColor
        passwordReapedTF.layer.cornerRadius = 5
        passwordReapedTF.clipsToBounds = true
        passwordReapedTF.isSecureTextEntry = true
        passwordReapedTF.placeholder = "请输入确认密码"
        passwordReapedTF.snp.makeConstraints { (m) in
            m.leading.trailing.height.equalTo(passwordTF)
            m.top.equalTo(passwordLabel.snp_bottom).offset(10)
        }
        
        self.view.addSubview(passwordReapedLabel)
        passwordReapedLabel.snp.makeConstraints { (m) in
            m.leading.trailing.height.equalTo(passwordLabel)
            m.top.equalTo(passwordReapedTF.snp_bottom).offset(10)
        }
        
        self.view.addSubview(loginBtn)
        loginBtn.setTitle("注册", for: .normal)
        loginBtn.setTitleColor(.black, for: .normal)
        loginBtn.layer.borderWidth = 1
        loginBtn.layer.borderColor = UIColor.gray.cgColor
        loginBtn.layer.cornerRadius = 5
        loginBtn.clipsToBounds = true
        loginBtn.snp.makeConstraints { (m) in
            m.leading.trailing.height.equalTo(passwordReapedTF)
            m.bottom.equalTo(passwordReapedLabel.snp_bottom).offset(50)
        }
        
        //与viewModel绑定
        setupViewModel()
        
        
    }
    
    func setupViewModel(){
        let viewModel = SignUpViewModel(input: (username: usernameTF.rx.text.orEmpty.asDriver(), password: passwordTF.rx.text.orEmpty.asDriver(), repeatePassword: passwordReapedTF.rx.text.orEmpty.asDriver(), loginTags: loginBtn.rx.tap.asSignal()), dependency: (networkService: NetWorkService(), signUpService: SignupService()))
        
    viewModel.validatedUsername.drive(usernameLabel.rx.validationResult).disposed(by: disposBag)
        
    viewModel.validatedPassword.drive(passwordLabel.rx.validationResult).disposed(by: disposBag)
        
    viewModel.validatedPasswordRepeate.drive(passwordReapedLabel.rx.validationResult).disposed(by: disposBag)
        
        viewModel.signupEnable.drive(onNext: { [weak self] valid in
            self?.loginBtn.isEnabled = valid
            self?.loginBtn.alpha = valid ? 1.0 : 0.3
        }).disposed(by: disposBag)
        
        viewModel.signUpResult.drive(onNext: { [weak self] result in
            self?.showMessage("注册" + (result ? "成功" : "失败") + "!")
        }).disposed(by: disposBag)
        
    }
    
    //详细提示框
    func showMessage(_ message: String) {
        let alertController = UIAlertController(title: nil,
                                                message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
}
